/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * giaoca.java
 *
 * Created on Jun 13, 2012, 7:00:07 PM
 */
package sniff;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.event.*;
import javax.swing.table.TableModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import java.util.Date;
/*
 *
 * @author trungdung
 */
public class giaoca extends javax.swing.JFrame {

    /** Creates new form giaoca */
    public giaoca(int userid) {
        initComponents();
        nvid=userid;
        makeQuery();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        gio = new javax.swing.JLabel();
        tongthu = new javax.swing.JLabel();
        the = new javax.swing.JLabel();
        cabox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        luongcung = new javax.swing.JLabel();
        doanhso = new javax.swing.JLabel();
        tongluong = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        thongbao = new javax.swing.JLabel();

        setTitle("Giao ca tiền giờ Game18+");

        mainPanel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Thẻ điện tử", null, null, null, null, null, null},
                {"Tiền giờ", null, null, null, null, null, null}
            },
            new String [] {
                "Tên Dịch Vụ", "Nhập thêm", "Tồn đầu", "Xuất", "Tồn cuối", "Giá", "Thành tiền"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit1 = new boolean [] {
                false, false, false, false, false, false, true
            };
            boolean[] canEdit2 = new boolean [] {
                false, false, false, false, false, false, true
            };
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                if (rowIndex==1) return canEdit2 [columnIndex];
                else return canEdit1 [columnIndex];
            }
        });
        jTable1.setRowHeight(25);
        jTable1.setRowSelectionAllowed(false);
        jScrollPane1.setViewportView(jTable1);
        jTable1.setShowGrid(true);
        jTable1.setGridColor(java.awt.Color.BLUE);
        //jTable1.getModel().addTableModelListener(ln);
        jTable1.getModel().addTableModelListener(new TableModelListener() {

            public void tableChanged(TableModelEvent e) {
                int row = e.getFirstRow();
                int column = e.getColumn();
                //if((column!=1)&&(column!=3)&&(column!=4)) return;
                TableModel model = (TableModel)e.getSource();
                String columnName = model.getColumnName(column);
                Integer data = (Integer)model.getValueAt(row, column);
                //System.out.println(model.getValueAt(row, 2));

                //ton cuoi thay doi => xuat thay doi

                if(column==4)
                model.setValueAt(-unull(data)+unull(model.getValueAt(row, 2))+unull(model.getValueAt(row, 1)), row, 3);

                //ton dau thay doi => xuat thay doi
                if(column==2)
                model.setValueAt(-unull(model.getValueAt(row, 4))+unull(model.getValueAt(row, 1))+unull(data), row, 3);

                //Nhap thay doi => Xuat thay doi
                if(column==1)
                model.setValueAt(-unull(model.getValueAt(row, 4))+unull(model.getValueAt(row, 2))+unull(data), row, 3);

                //Xuat thay doi => Thanh tien
                if(column==3)
                model.setValueAt(unull(model.getValueAt(row, 3)), row, 6);

                //the dien tu -> the, tongthu thay doi
                if(row==0) {
                    tienthe=unull(model.getValueAt(0, 6));
                    the.setText(" Tiền thẻ         : "+Integer.toString(tienthe));
                    newupdate();

                    //check chiet khau the
                    int td,tc,nt;
                    float tile=0;
                    td=unull(model.getValueAt(0,2));
                    tc=unull(model.getValueAt(0,4));
                    nt=unull(model.getValueAt(0,1));
                    if (tienthe !=0) tile=(tienthe-(td+nt-tc))*100/tienthe;
                    else thongbao.setText("Nhập thẻ");
                    if ((tile<5.2)||(tile>6.8))
                    thongbao.setText("Kiểm tra lại tiền thẻ");
                    else thongbao.setText("OK");

                }
                if(row==1) {
                    tiengio=unull(model.getValueAt(1, 6));
                    gio.setText(" Tiền giờ          : "+Integer.toString(tiengio));
                    newupdate();
                }
            }

        });

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Tài khoản", "Số tiền", "Ngày", "Giờ", "Nhân viên"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        model2 = (DefaultTableModel)jTable2.getModel();
        jScrollPane2.setViewportView(jTable2);

        jLabel3.setText("Giao dịch âm:");

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 788, Short.MAX_VALUE)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 788, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addGap(0, 254, Short.MAX_VALUE))
            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                    .addGap(0, 161, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        gio.setText(" Tiền giờ          :");
        gio.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        tongthu.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        tongthu.setForeground(new java.awt.Color(51, 51, 255));
        tongthu.setText(" Tổng thu    :");
        tongthu.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        the.setText(" Tiền thẻ         :");
        the.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        cabox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "" }));
        cabox.setSelectedIndex(0);
        cabox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                caboxActionPerformed(evt);
            }
        });

        jLabel1.setText("Thời gian :");

        luongcung.setText(" Lương cứng  :");
        luongcung.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        doanhso.setText(" Doanh số      :");
        doanhso.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        tongluong.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        tongluong.setForeground(new java.awt.Color(51, 51, 255));
        tongluong.setText(" Tổng lương:");
        tongluong.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel2.setText(" @Free License by Trung Dũng - Yahoo!ID: matran19900");

        thongbao.setForeground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 372, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 655, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cabox, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(tongthu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(gio, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                            .addComponent(the, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(luongcung, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(doanhso, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(tongluong, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(thongbao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(249, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cabox, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(gio, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(the, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tongthu, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(luongcung, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(doanhso, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tongluong, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(61, 61, 61)
                .addComponent(thongbao, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 92, Short.MAX_VALUE)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(35, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void caboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_caboxActionPerformed
        // TODO add your handling code here:
        caindex=cabox.getSelectedIndex();
        newupdate();

    }//GEN-LAST:event_caboxActionPerformed
    
    private Integer unull(Object obj) {
        if(obj!=null) return Integer.parseInt(obj.toString());
        else return 0;
    }
    
    private void newupdate() {
        tong=tiengio+tienthe;
        luongdoanhso=tienthe*2/100;
        luong=tienluongcung+luongdoanhso;
        tongthu.setText(" Tổng thu       : "+tong);
        luongcung.setText(" Lương cứng  : "+tienluongcung);
        doanhso.setText(" Doanh số      : "+luongdoanhso);
        tongluong.setText(" Tổng lương   : "+luong);
    }
    private void resetTable() {
        for(int i=0;i<jTable1.getRowCount();i++)
            ((DefaultTableModel)jTable1.getModel()).removeRow(i);  
    }
    
    private void makeQuery() {
        try
        {
            Format  formatter = new SimpleDateFormat("yyyy-MM-dd");
           // long unixTime=Date.getTime()/1000L;
           // Date today=new Date();
           // long unixTime=today.getTime()/1000L;
           // Date yesterday = new Date(unixTime-86400);
            Calendar cal = Calendar.getInstance();
            String today=formatter.format(cal.getTime())+" 23:59:59";
            cal.add(Calendar.DATE, -1);
            String yesterday=formatter.format(cal.getTime())+" 19:00:00";
            
            Connection con;
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            con= DriverManager.getConnection("jdbc:mysql://localhost/ddm","abc123","Khongbiet$!@#$");
            
            //Giao dich am
            String query = "select paymenttb.VoucherId,usertb.UserId,usertb.UserName from usertb,paymenttb where usertb.UserId=paymenttb.UserId and CONCAT(paymenttb.VoucherDate,' ',paymenttb.VoucherTime)<? and CONCAT(paymenttb.VoucherDate,' ',paymenttb.VoucherTime)>? and paymenttb.Amount<0";
            PreparedStatement preparedStmt = con.prepareStatement(query);
            preparedStmt.setString(1, today);
            preparedStmt.setString(2, yesterday);
            // execute the java preparedstatement
            ResultSet rs=preparedStmt.executeQuery();
                       //     System.out.println(today +'\n'+yesterday);

            while (rs.next())
            {
                String query2 = "select paymenttb.Amount,paymenttb.VoucherDate,paymenttb.VoucherTime,(select usertb.UserName from usertb where usertb.UserId=paymenttb.StaffId) as staff from paymenttb where paymenttb.UserId=? and CONCAT(paymenttb.VoucherDate,' ',paymenttb.VoucherTime)<? and CONCAT(paymenttb.VoucherDate,' ',paymenttb.VoucherTime)>?";
                PreparedStatement preparedStmt2 = con.prepareStatement(query2);
                preparedStmt2.setString(1, rs.getString(2));
                preparedStmt2.setString(2, today);
                preparedStmt2.setString(3, yesterday);
                // execute the java preparedstatement
                ResultSet rs2=preparedStmt2.executeQuery();  
                
                while (rs2.next()) 
                {
                    model2.addRow(new Object[]{rs.getString(3),rs2.getString(1),rs2.getString(2),rs2.getString(3),rs2.getString(4)});
                    
                }
                model2.addRow(new Object[]{" "," "," "," "," "});
            }           
            con.close();
        }
        catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e)
        {
            e.printStackTrace();
        }
    }
   
    private String dau;
    private String cuoi;
    private int caindex;
    private int nvid;
  //  boolean stt_dalaydulieu=false;
    int tienthe=0,tiengio=0,tong=0,luong=0,luongdoanhso=0;
    int tienluongcung=100000;
    DefaultTableModel model2;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cabox;
    private javax.swing.JLabel doanhso;
    private javax.swing.JLabel gio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JLabel luongcung;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JLabel the;
    private javax.swing.JLabel thongbao;
    private javax.swing.JLabel tongluong;
    private javax.swing.JLabel tongthu;
    // End of variables declaration//GEN-END:variables
    //private listener ln=new listener();
}
